package net.zigin.nhi.could.waste.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    //队列名称
    public final static String queue = "waste_queue";
    public final static String dcrq = "do_collect_red_queue";
    public final static String idrq = "in_depot_red_queue";
    public final static String odrq = "out_depot_red_queue";
    public final static String dcyq = "do_collect_yellow_queue";
    public final static String idyq = "in_depot_yellow_queue";
    public final static String odyq = "out_depot_yellow_queue";

    //交换机名称
    public final static String exchangeName = "waste_exchange";

    // routingKey
    public final static String routingKey  = "waste_key";
    public final static String dcrk = "do_collect_time_red";
    public final static String idrk = "in_depot_time_red";
    public final static String odrk = "out_depot_time_red";
    public final static String dcyk = "do_collect_time_yellow";
    public final static String idyk = "in_depot_time_yellow";
    public final static String odyk = "out_depot_time_yellow";


    //死信消息队列名称 todo 可以多个
    public final static String deal_queue = "waste_deal_queue";
    public final static String dcrdq = "do_collect_red_dead_quere";
    public final static String idrdq = "in_depot_red_dead_quere";
    public final static String odrdq = "out_depot_red_dead_quere";
    public final static String dcydq = "do_collect_yellow_dead_quere";
    public final static String idydq = "in_depot_yellow_dead_quere";
    public final static String odydq = "out_depot_yellow_dead_quere";



    //死信交换机名称
    public final static String deal_exchangeName = "waste_deal_exchange";

    //死信 routingKey
    public final static String dead_RoutingKey  = "waste_dead_routing_key";
    public final static String dcrdk = "do_collect_red_dead_key";
    public final static String idrdk = "in_depot_red_dead_key";
    public final static String odrdk = "out_depot_red_dead_key";
    public final static String dcydk = "do_collect_yellow_dead_key";
    public final static String idydk = "in_depot_yellow_dead_key";
    public final static String odydk = "out_depot_yellow_dead_key";


    //死信队列 交换机标识符
    public static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";

    //死信队列交换机绑定键标识符
    public static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Bean
    public Queue wasteQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, dead_RoutingKey);
        return new Queue(RabbitMqConfig.queue, true, false, false, args);
    }

    @Bean
    public Queue doCollectRedQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, dcrdk);
        return new Queue(RabbitMqConfig.dcrq, true, false, false, args);
    }

    @Bean
    public Queue inDepotRedQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, idrdk);
        return new Queue(RabbitMqConfig.idrq, true, false, false, args);
    }

    @Bean
    public Queue outCollectRedQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, odrdk);
        return new Queue(RabbitMqConfig.odrq, true, false, false, args);
    }

    @Bean
    public Queue doCollectYellowQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, dcydk);
        return new Queue(RabbitMqConfig.dcyq, true, false, false, args);
    }

    @Bean
    public Queue inDepotYellowQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, idydk);
        return new Queue(RabbitMqConfig.idyq, true, false, false, args);
    }

    @Bean
    public Queue outCollectYellowQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deal_exchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, odydk);
        return new Queue(RabbitMqConfig.odyq, true, false, false, args);
    }


    @Bean
    public DirectExchange wasteExchange() {
        return new DirectExchange(RabbitMqConfig.exchangeName);
    }

    @Bean
    public Binding bindingDirectExchange(   ) {
        return BindingBuilder.bind(wasteQueue()).to(wasteExchange()).with(routingKey);
    }

    @Bean
    public Binding bindingDirectExchange1(   ) {
        return BindingBuilder.bind(doCollectRedQueue()).to(wasteExchange()).with(dcrk);
    }

    @Bean
    public Binding bindingDirectExchange2(   ) {
        return BindingBuilder.bind(inDepotRedQueue()).to(wasteExchange()).with(idrk);
    }

    @Bean
    public Binding bindingDirectExchange3(   ) {
        return BindingBuilder.bind(outCollectRedQueue()).to(wasteExchange()).with(odrk);
    }

    @Bean
    public Binding bindingDirectExchange4(   ) {
        return BindingBuilder.bind(doCollectYellowQueue()).to(wasteExchange()).with(dcyk);
    }

    @Bean
    public Binding bindingDirectExchange5(   ) {
        return BindingBuilder.bind(inDepotYellowQueue()).to(wasteExchange()).with(idyk);
    }

    @Bean
    public Binding bindingDirectExchange6(   ) {
        return BindingBuilder.bind(outCollectYellowQueue()).to(wasteExchange()).with(odyk);
    }

    @Bean
    public Queue wasteDeadQueue() {
        return new Queue(deal_queue, true);
    }

    @Bean
    public Queue wasteDeadQueue1() {
        return new Queue(dcrdq, true);
    }

    @Bean
    public Queue wasteDeadQueue2() {
        return new Queue(idrdq, true);
    }

    @Bean
    public Queue wasteDeadQueue3() {
        return new Queue(odrdq, true);
    }

    @Bean
    public Queue wasteDeadQueue4() {
        return new Queue(dcydq, true);
    }

    @Bean
    public Queue wasteDeadQueue5() {
        return new Queue(idydq, true);
    }

    @Bean
    public Queue wasteDeadQueue6() {
        return new Queue(odydq, true);
    }

    @Bean
    public DirectExchange wasteDeadExchange() {
        return new DirectExchange(deal_exchangeName);
    }

    @Bean
    public Binding bindingDeadExchange() {
        return BindingBuilder.bind(wasteDeadQueue()).to(wasteDeadExchange()).with(dead_RoutingKey);
    }

    @Bean
    public Binding bindingDeadExchange1() {
        return BindingBuilder.bind(wasteDeadQueue1()).to(wasteDeadExchange()).with(dcrdk);
    }

    @Bean
    public Binding bindingDeadExchange2() {
        return BindingBuilder.bind(wasteDeadQueue2()).to(wasteDeadExchange()).with(idrdk);
    }

    @Bean
    public Binding bindingDeadExchange3() {
        return BindingBuilder.bind(wasteDeadQueue3()).to(wasteDeadExchange()).with(odrdk);
    }

    @Bean
    public Binding bindingDeadExchange4() {
        return BindingBuilder.bind(wasteDeadQueue4()).to(wasteDeadExchange()).with(dcydk);
    }

    @Bean
    public Binding bindingDeadExchange5() {
        return BindingBuilder.bind(wasteDeadQueue5()).to(wasteDeadExchange()).with(idydk);
    }

    @Bean
    public Binding bindingDeadExchange6() {
        return BindingBuilder.bind(wasteDeadQueue6()).to(wasteDeadExchange()).with(odydk);
    }

}